package org.tio.sitexxx.service.init;

import org.redisson.Redisson;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.codec.FstCodec;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.tio.sitexxx.service.topic.TopicCommonListener;
import org.tio.sitexxx.service.vo.Const.Topic;
import org.tio.sitexxx.service.vo.topic.TopicVo;
import org.tio.utils.jfinal.P;

import cn.hutool.core.util.StrUtil;

/**
 * @author tanyaowu 
 * 2016年11月8日 下午3:48:02
 */
public class RedisInit {

	private static RedissonClient redisson;

	public static RedissonClient get() {
		return redisson;
	}

	public static void shutdown() {
		redisson.shutdown();
	}

	public static void init() {

		Config config = new Config();

		String address = "redis://" + P.get("redis.ip") + ":" + P.get("redis.port");
		SingleServerConfig singleServerConfig = config.useSingleServer().setAddress(address).setConnectionPoolSize(32).setConnectionMinimumIdleSize(16);

		String pwd = P.get("redis.password");
		if (StrUtil.isNotBlank(pwd)) {
			singleServerConfig.setPassword(pwd);
		}

		config.setCodec(new FstCodec());
		config.setLockWatchdogTimeout(1000 * 30); //默认情况下，看门狗的检查锁的超时时间是30秒钟
		redisson = Redisson.create(config);

		RTopic topic = redisson.getTopic(Topic.COMMON_TOPIC);
		topic.addListener(TopicVo.class, TopicCommonListener.ME);
	}

}
